﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class TagModule
    {
        private readonly KeRui.DAL.TagModule dal = new KeRui.DAL.TagModule();

        public int Add(KeRui.Model.TagModule model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.TagModule> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.TagModule> list = new List<KeRui.Model.TagModule>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.TagModule item = new KeRui.Model.TagModule();
                    if (dt.Rows[i]["ModuleID"].ToString() != "")
                    {
                        item.ModuleID = int.Parse(dt.Rows[i]["ModuleID"].ToString());
                    }
                    item.ModuleName = dt.Rows[i]["ModuleName"].ToString();
                    item.DataTable = dt.Rows[i]["DataTable"].ToString();
                    if (dt.Rows[i]["TopNum"].ToString() != "")
                    {
                        item.TopNum = int.Parse(dt.Rows[i]["TopNum"].ToString());
                    }
                    item.StrWhere = dt.Rows[i]["StrWhere"].ToString();
                    item.OrderBy = dt.Rows[i]["OrderBy"].ToString();
                    item.Direction = dt.Rows[i]["Direction"].ToString();
                    item.Code = dt.Rows[i]["Code"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int ModuleID)
        {
            this.dal.Delete(ModuleID);
        }

        public bool Exists(string moduleName)
        {
            return this.dal.Exists(moduleName);
        }

        public DataTable GetDBList()
        {
            return this.dal.GetDBList();
        }

        public DataTable GetFieldsByTableID(int tableID)
        {
            return this.dal.GetFieldsByTableID(tableID);
        }

        public List<KeRui.Model.TagModule> GetList(string strWhere)
        {
            string strOrder = "ModuleID DESC";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("TagModule", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.TagModule> GetListByCache(string strCondition)
        {
            string cacheKey = "TagModuleList-" + strCondition;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(strCondition);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.TagModule>) cache;
        }

        public KeRui.Model.TagModule GetModel(int ModuleID)
        {
            return this.dal.GetModel(ModuleID);
        }

        public DataTable GetModelByName(string moduleName)
        {
            return this.dal.GetModelByName(moduleName);
        }

        public string GetTableIDByTableName(string tableName)
        {
            return this.dal.GetTableIDByTableName(tableName);
        }

        public string GetTableNameByTableID(int tableID)
        {
            return this.dal.GetTableNameByTableID(tableID);
        }

        public void Update(KeRui.Model.TagModule model)
        {
            this.dal.Update(model);
        }
    }
}

